package com.example.tests;

import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

import static org.testng.Assert.fail;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.example.fw.AccountHelper;
import com.example.fw.AdminHelper;
import com.example.fw.JamesHelper;
import com.example.fw.User;

public class SignupTest extends TestBase {
	public User user = new User()
		.setLogin("vasa1")
		.setEmail("testuser2@localhost.localdomain")
		.setPassword("123456");
//	public User user = new User()
//		.setLogin("administrator")
//		.setEmail("")
//		.setPassword("root");
	private JamesHelper james;
	private AccountHelper accHelper;
	private AdminHelper admin;
	
//	@BeforeClass
	public void createMailUser() {
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		admin = app.getAdminHelper();
		if (!james.doesUserExist(user.login)) {
			james.createUser(user.login, user.password);
		}
	}
	
//	@AfterClass
	public void deleteMailUser() {
		if (james.doesUserExist(user.login)) {
			james.deleteUser(user.login);
		}
	}
	
	@Test
	public void newUserShouldSignup() throws Exception {
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		admin = app.getAdminHelper();
		admin.deleteUserIfExists(user.login);
//		accHelper.signup(user);
//		accHelper.login(user);
//		assertThat(accHelper.loggedUser(), equalTo(user.login));
	}
	
//	@Test
	public void existingUserShouldNotSignup() throws Exception {
		james = app.getJamesHelper();
		accHelper = app.getAccountHelper();
		admin = app.getAdminHelper();
		if (!admin.userExist(user.login)) {
			admin.createUser(user.login);
		}
		try {
			accHelper.signup(user);
		} catch (Exception e) {
			assertThat(e.getMessage(), containsString("That username is already being used."));
			return;
		}
		fail("Existing user shouldn't be able to signup. Exception expected.");
	}

}
